package com.guilin.controller.front;

import com.guilin.common.constant.Constants;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.domain.SurveyQuestionnaire;
import com.guilin.system.service.SurveyQuestionnaireService;
import com.guilin.system.service.SurveyResponseService;
import com.guilin.vo.SurveyQuestionnaireDetailVO;
import com.guilin.vo.SurveyResponseSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户端获取可填写的问卷列表（已发布状态）
 */
@RestController
@RequestMapping("/user/questionnaire")
public class UserSurveyQuestionnaireControllerFront {

    @Autowired
    private SurveyQuestionnaireService surveyQuestionnaireService;

    @Autowired
    private SurveyResponseService surveyResponseService;

    @GetMapping("/list")
    public AjaxResult getFillableQuestionnaireList() {
        // 筛选状态为“已发布”的问卷
        SurveyQuestionnaire query = new SurveyQuestionnaire();
        query.setStatus(Constants.SURVEY_ONE);
        List<SurveyQuestionnaire> list = surveyQuestionnaireService.selectSurveyQuestionnaireList(query);
        return AjaxResult.success(list);
    }

    /**
     * 获取问卷详情及关联题目
     */
    @GetMapping("/{questionnaireId}/detail")
    public AjaxResult getQuestionnaireWithQuestions(@PathVariable Long questionnaireId) {

        // 返回包含题目列表的问卷详情
        SurveyQuestionnaireDetailVO detail = surveyQuestionnaireService.selectQuestionnaireWithQuestions(questionnaireId);
        return AjaxResult.success(detail);
    }

    /**
     * 提交问卷回答
     */
    @PostMapping("/submit")
    public AjaxResult submitResponse(@RequestBody SurveyResponseSubmitVO submitVO) {
        // submitVO需包含问卷ID、回答者信息、题目答案列表等
        Long responseId = surveyResponseService.insertUserResponse(submitVO);
        return AjaxResult.success("提交成功", responseId);
    }
}
